package com.wyh.controller.groupOne.resource;


import com.github.pagehelper.PageInfo;
import com.wyh.common.enums.ResultCodeEnum;
import com.wyh.common.result.Result;
import com.wyh.domain.dto.groupOne.resource.*;

import com.wyh.domain.po.groupOne.resource.MasterDataResource;
import com.wyh.domain.vo.groupOne.contractManagement.ContractAddVO;
import com.wyh.domain.vo.groupOne.utils.MasterDataTreeNode;
import com.wyh.service.groupOne.contractManagement.MasterDataIContractService;
import com.wyh.service.serviceImpl.groupOne.resource.MasterDataMasterDataResourceServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.wyh.common.enums.ResultCodeEnum.*;
import static com.wyh.common.enums.ResultCodeEnum.KEYWORDS_ERROR;

@RestController
@RequestMapping("/masterDataResource")
@Api(tags = "资源管理API")
public class MasterDataResourceController {
    @Autowired
    private MasterDataMasterDataResourceServiceImpl resourceService;
    @Autowired
    private MasterDataIContractService contractService;

    /**
     * 新增
     */
    @PostMapping("/add")
    @ApiOperation("新增资源")
    @ApiImplicitParam(name = "masterDataResource", value = "资源", required=true, dataType = "MasterDataResource_1")
    public Result add(@RequestBody MasterDataResource masterDataResource,
                      @RequestHeader(value = "authorization", required = false) String authorization) {
        if (masterDataResource == null) {
            return Result.error(PARAM_ERROR);
        }
        resourceService.insert(masterDataResource);
        return Result.success(ResultCodeEnum.ADD_SUCCESS);
    }

    /**
     * 修改
     */
    @PostMapping("/updateById")
    @ApiOperation("修改资源")
    @ApiImplicitParam(name = "masterDataResource", value = "资源", required=true, dataType = "MasterDataResource_1")
    public Result updateById(@RequestBody MasterDataResource masterDataResource,
                             @RequestHeader(value = "authorization", required = false) String authorization) {
        if (masterDataResource == null) {
            return Result.error(PARAM_ERROR);
        }
        resourceService.updateById(masterDataResource);
        return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
    }

    /**
     * 删除
     */
    @GetMapping("/delete")
    @ApiOperation("删除资源")
    @ApiImplicitParam(name = "id", value = "资源id", required=true, dataType = "Integer")
    public Result deleteById(@RequestParam("id") Integer id,
                             @RequestHeader(value = "authorization", required = false) String authorization) {
        if (id == null) {
            return Result.error(PARAM_ERROR);
        }
        resourceService.deleteById(id);
        return Result.success(ResultCodeEnum.ORGANIZATION_REMOVE_SUCCESS);
    }

    @PostMapping("/selectAllResource")
    @ApiOperation("树上查询所有资源")
    public Result selectAllResource(@RequestBody MasterDataResourceTypeDTO resourceTypeDTO,
                                    @RequestHeader(value = "authorization", required = false) String authorization) {
        List<ResourceDetailDTO> list = resourceService.getAllDetails(resourceTypeDTO);
        return Result.success(list);
    }

    @GetMapping("/getSplitResource")
    @ApiOperation("获取当前资源的拆分信息")
    public Result getSplitResource(@RequestParam("id") Integer id,
                                   @RequestHeader(value = "authorization", required = false) String authorization){
        ResourceSplitShowDTO splitResource = resourceService.getSplitResource(id);
        return Result.success(splitResource);
    }

    @PostMapping("/splitResource")
    @ApiOperation("拆分资源")
    public Result splitResource(@RequestBody ResourceSplitDTO resourceSplitDTO,
                                @RequestHeader(value = "authorization", required = false) String authorization) {
        resourceService.splitResource(resourceSplitDTO);
        return Result.success("拆分成功");
    }

    @GetMapping("/getCombineResource")
    @ApiOperation("获取当前资源的合并信息")
    @ApiImplicitParam(name = "id", value = "资源id", required=true, dataType = "Integer")
    public Result getCombineResource(@RequestParam("id") Integer id,
                                     @RequestHeader(value = "authorization", required = false) String authorization) {
        ResourceCombineShowDTO combineResource = resourceService.getCombineResource(id);
        return Result.success(combineResource);
    }

    @GetMapping("/selectLayerAllResource")
    @ApiOperation("合并资源中查询当前资源楼层下的所有资源")
    @ApiImplicitParam(name = "id", value = "资源id", required=true, dataType = "Integer")
    public Result selectLayerAllResource(@RequestParam("id") Integer id,
                                         @RequestHeader(value = "authorization", required = false) String authorization) {
        List<MasterDataResource> list = resourceService.selectLayerAllResource(id);
        return Result.success(list);
    }

    @PostMapping("/combineResource")
    @ApiOperation("合并资源")
    public Result combineResource(@RequestBody ResourceCombineDTO resourceCombineDTO,
                                  @RequestHeader(value = "authorization", required = false) String authorization) {
        resourceService.combineResource(resourceCombineDTO);
        return Result.success("合并成功");
    }

    @GetMapping("/selectResourceCustomer")
    @ApiOperation("查询资源下的客户信息")
    @ApiImplicitParam(name = "id", value = "资源id", required=true, dataType = "Integer")
    public Result selectResourceCustomer(@RequestParam("id") Integer id,
                                         @RequestHeader(value = "authorization", required = false) String authorization) {
        List<ResourceCustomerDTO> resourceCustomerDTOS = resourceService.selectResourceCustomer(id);
        return Result.success(resourceCustomerDTOS);
    }

    @GetMapping("/selectResourceCustomerHistory")
    @ApiOperation("查询资源下的历史客户信息")
    @ApiImplicitParam(name = "id", value = "资源id", required=true, dataType = "Integer")
    public Result selectResourceCustomerHistory(@RequestParam("id") Integer id,
                                                @RequestHeader(value = "authorization", required = false) String authorization) {
        List<ResourceCustomerDTO> resourceCustomerDTOS = resourceService.selectResourceCustomerHistory(id);
        return Result.success(resourceCustomerDTOS);
    }

    /**
     * 分页查询
     */
    @PostMapping("/selectPage")
    @ApiOperation("分页查询")
    public Result selectPage(@RequestBody MasterDataResourceTypeDTO masterDataResourceTypeDTO,
                             @RequestHeader(value = "authorization", required = false) String authorization) {
        PageInfo<ResourceDetailDTO> page = resourceService.selectPage(masterDataResourceTypeDTO);
        return Result.success(page);
    }

    // 构建资源管理的菜单树,只包含组织，园区，楼栋，楼层
    @GetMapping("/getResourceTree")
    @ApiOperation("获取资源树")
    public Result getResourceTree(@RequestHeader(value = "authorization", required = false) String authorization) {
        List<MasterDataTreeNode> tree = resourceService.generateResourceTree();
        if (tree == null) {
            Result.error(SYSTEM_ERROR);
        }
        return Result.success(tree);
    }

    // 获取模糊查询前端实时响应的树的接口
    @GetMapping("/searchResourceTree")
    @ApiOperation("模糊查询动态返回菜单树")
    @ApiImplicitParam(name = "keyword", value = "查询关键字", required=true)
    public Result searchResourceTree(@RequestParam("keyword") String keyword,
                                     @RequestHeader(value = "authorization", required = false) String authorization) {
        if (keyword == null) {
            return Result.error(ORGANIZATION_FIND_ERROR);
        }
        List<MasterDataTreeNode> resourceTree = resourceService.searchResourceTree(keyword);
        if (resourceTree == null || resourceTree.isEmpty()) {
            return Result.success(KEYWORDS_ERROR);
        }
        return Result.success(resourceTree);
    }

    // 输入条件查询后，会查一遍所有的数据，存入redis，然后取redis条件查询
    @PostMapping("/getResourceByCondition")
    @ApiOperation("条件查询资源")
    public Result getAllResourceByRootNode(@RequestBody MasterDataResource masterDataResource,
                                           @RequestHeader(value = "authorization", required = false) String authorization) {
        if (masterDataResource == null) {
            return Result.error(PARAM_ERROR);
        }
        PageInfo<ResourceDetailDTO> resourceDetailDTOList = resourceService.selectByCondition(masterDataResource);
        return Result.success(resourceDetailDTOList);
    }

    // 根据资源id查询缴费记录的接口
    @ApiOperation("查询当前资源的所有缴费记录（包括条件查询）")
    @PostMapping("/getAllCharging")
    public Result getAllCharging(@RequestBody PaymentHistory paymentHistory,
                                 @RequestHeader(value = "authorization", required = false) String authorization) {
        if (paymentHistory == null) {
            return Result.error(PARAM_ERROR);
        }
        List<PaymentHistory> allCharging = resourceService.getAllCharging(paymentHistory);
        return Result.success(allCharging);
    }

    @ApiOperation("查询详情中的三表信息")
    @ApiImplicitParam(name = "id", value = "资源id", required=true)
    @GetMapping("/getAllThreeMeterInfo")
    public Result getAllThreeMeterInfo(@RequestParam("id") Integer id,
                                       @RequestHeader(value = "authorization", required = false) String authorization) {
        if (id == null) {
            return Result.error(PARAM_ERROR);
        }
        List<ThreeMeterInfo> threeMeterInfos = resourceService.getThreeMeterInfos(id);
        return Result.success(threeMeterInfos);
    }

    @ApiOperation("停用三表信息")
    @ApiImplicitParam(name = "id", value = "三表id", required=true)
    @GetMapping("/deleteThreemeter")
    public Result deleteThreemeter(@RequestParam("id") Integer threemeterId,
                                   @RequestHeader(value = "authorization", required = false) String authorization) {
        if (threemeterId == null) {
            return Result.error(PARAM_ERROR);
        }
        resourceService.deleteThreemeterById(threemeterId);
        return Result.success(ORGANIZATION_REMOVE_SUCCESS);
    }

    @ApiOperation("换表")
    @PostMapping("/replaceThreemeter")
    public Result replaceThreemeter(@RequestBody @Validated ThreeMeterInfo threeMeterInfo,
                                    @RequestHeader(value = "authorization", required = false) String authorization) {
        if (threeMeterInfo == null) {
            return Result.error(PARAM_ERROR);
        }
        resourceService.replaceThreemeter(threeMeterInfo);
        return Result.success(REPLACEED_SUCCESS);
    }

    @PostMapping("/movein")
    public Result movein(@RequestBody ContractAddVO contractAddVO,
                         @RequestHeader(value = "authorization", required = false) String authorization) {
        return contractService.addContract(contractAddVO);

    }

}
